//made partly with help from ChatGBT //arduino program to control a LED and button //LED is on pin 9 //button is on pin 0 //when button is pressed once LED turns on //when button is pressed twice LED turns off //when button is pressed three times LED blinks three times //when button is pressed four times LED blinks like crazy #include #define BUTTON D0 #define LED_PIN 9 // Change this to the appropriate pin for your setup #define NUM_LEDS 72 // Number of LEDs in your strip const int FIRST_LEDS[] = {0, 11, 12, 23, 24, 35, 36, 47, 48, 59, 60, 71, }; // LED indices for the first row const int SECOND_LEDS[] = {1, 10, 13, 22, 25, 34, 37, 46, 49, 58, 61, 70, }; // LED indices for the second row const int THIRD_LEDS[] = {2, 9, 14, 21, 26, 33, 38, 45, 50, 57, 62, 69, }; // LED indices for the third row const int FORTH_LEDS[] = {3, 8, 15, 20, 27, 32, 39, 44, 51, 56, 63, 68, }; // LED indices for the fourh row const int FIFTH_LEDS[] = {4, 7, 16, 19, 28, 31, 40, 43, 52, 55, 64, 67, }; // LED indices for the fifth row const int SIXTH_LEDS[] = {5, 6, 17, 18, 29, 30, 41, 42, 53, 54, 65, 66, }; // LED indices for the sixth row CRGB leds[NUM_LEDS]; //variable to keep track of how manny button presses int buttonPresses = 0; void setup() { Serial.begin(115200); delay(10); pinMode(LED_PIN, OUTPUT); pinMode(BUTTON, INPUT_PULLUP); FastLED.addLeds(leds, NUM_LEDS); FastLED.show(); // Initialize the LED strip with the initial colors } //loop function void loop() { //if button is pressed if (digitalRead(BUTTON) == HIGH) { //increment buttonPresses buttonPresses++; //if buttonPresses is greater than 5 Serial.println(buttonPresses); if (buttonPresses > 3) { //reset buttonPresses to 0 buttonPresses = 0; } //if buttonPresses is 1 if (buttonPresses == 1) { randomColorChange(); } //if buttonPresses is 2 if (buttonPresses == 2) { //turn off LED // Assign color to the first set of LEDs (first row) setLEDsColor(FIRST_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(214, 2, 112)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the second set of LEDs (second row) setLEDsColor(SECOND_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(214, 2, 112)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the third set of LEDs (third row) setLEDsColor(THIRD_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(128, 0, 180)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the fourtht set of LEDs (fourth row) setLEDsColor(FORTH_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(128, 0, 180)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the fifth set of LEDs (fifth row) setLEDsColor(FIFTH_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(0, 0, 255)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the sixth set of LEDs (sixth row) setLEDsColor(SIXTH_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(0, 0, 255)); // Show the updated colors on the LED strip FastLED.show(); delay(0); } //if buttonPresses is 3 if (buttonPresses == 3) { //blink LED three times for (int i = 0; i < 3; i++) { // Assign color to the first set of LEDs (first row) setLEDsColor(FIRST_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(255, 0, 0)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the second set of LEDs (second row) setLEDsColor(SECOND_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(255, 128, 0)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the third set of LEDs (third row) setLEDsColor(THIRD_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(255, 255, 0)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the fourth set of LEDs (fourth row) setLEDsColor(FORTH_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(0, 255, 0)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the fifth set of LEDs (fifth row) setLEDsColor(FIFTH_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(0, 0, 255)); // Show the updated colors on the LED strip FastLED.show(); delay(0); // Assign color to the sixth set of LEDs (sixth row) setLEDsColor(SIXTH_LEDS, sizeof(FIRST_LEDS) / sizeof(FIRST_LEDS[0]), CRGB(128, 0, 128)); // Show the updated colors on the LED strip FastLED.show(); delay(0); } } //wait for button to be released while (digitalRead(BUTTON) == HIGH) { } } } void randomColorChange() { for (int i = 0; i < 200; i++) { // Randomly change the colors of LEDs between green and purple rapidly for (int j = 0; j < NUM_LEDS; j++) { if (random(2) == 0) { leds[j] = CRGB::Green; } else { leds[j] = CRGB::Purple; } } FastLED.show(); delay(30); } } void setLEDsColor(const int* ledIndices, int numLEDs, const CRGB& color) { for (int i = 0; i < numLEDs; i++) { leds[ledIndices[i]] = color; } } void setLEDsColor(const int* ledIndices, const CRGB& color) { int numLEDs = sizeof(ledIndices) / sizeof(ledIndices[0]); for (int i = 0; i < numLEDs; i++) { leds[ledIndices[i] - 1] = color; // Subtract 1 from LED index to account for array indexing starting from 0 } }